﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Portal.Core;

namespace Portal.AssetTn.Model
{
    public class AssetHistoryCls
    {
        public string AssetListHistoryId;
        public DateTime ActionDate;
        public string Subject;
        public string Owner;
    }


    public class AssetHistoryParser
    {
        public static AssetHistoryCls CreateInstance()
        {
            //tao 1 class trang....
            AssetHistoryCls oAssetHistoryCls = new AssetHistoryCls();
            return oAssetHistoryCls;
        }

        public static AssetHistoryCls ParseFromXml(string XmlData)
        {
            DataSet ds = XmlUtility.GetDataSetFromXml(XmlData);
            AssetHistoryCls oAssetHistoryCls = ParseFromDataRow(ds.Tables[0].Rows[0]);
            return oAssetHistoryCls;
        }


        public static AssetHistoryCls ParseFromDataRow(DataRow dr)
        {
            AssetHistoryCls oAssetHistoryCls = new AssetHistoryCls();

            if (dr["AssetListHistoryId"] != DBNull.Value)
            {
                oAssetHistoryCls.AssetListHistoryId = (string)dr["AssetListHistoryId"];
            }
            if (dr["ActionDate"] != DBNull.Value)
            {
                oAssetHistoryCls.ActionDate = (DateTime)dr["ActionDate"];
            }
            if (dr["Subject"] != DBNull.Value)
            {
                oAssetHistoryCls.Subject = (string)dr["Subject"];
            }
            if (dr["Owner"] != DBNull.Value)
            {
                oAssetHistoryCls.Owner = (string)dr["Owner"];
            }

            return oAssetHistoryCls;
        }

        public static AssetHistoryCls[] ParseFromDataTable(DataTable dtTable)
        {
            AssetHistoryCls[] AssetHistoryCls = new AssetHistoryCls[dtTable.Rows.Count];
            for (int iIndex = 0; iIndex < dtTable.Rows.Count; iIndex++)
            {
                AssetHistoryCls[iIndex] = ParseFromDataRow(dtTable.Rows[iIndex]);
            }

            return AssetHistoryCls;
        }

        public static DataSet CreateDataset()
        {
            DataSet ds = new DataSet("AssetHistory");
            ds.Tables.Add("Info");
            ds.Tables["Info"].Columns.Add("AssetListHistoryId");
            ds.Tables["Info"].Columns.Add("ActionDate");
            ds.Tables["Info"].Columns.Add("Subject");
            ds.Tables["Info"].Columns.Add("Owner");

            return ds;
        }

        public static string GetXml(AssetHistoryCls oAssetHistoryCls)
        {
            DataSet ds = CreateDataset();
            ds.Tables["Info"].Rows.Add(new object[]{
                        oAssetHistoryCls.AssetListHistoryId,
                        oAssetHistoryCls.ActionDate,
                        oAssetHistoryCls.Subject,
                        oAssetHistoryCls.Owner,
                    });
            return ds.GetXml();
        }

        public static string GetXml(DataRow dr)
        {
            AssetHistoryCls oAssetHistory = ParseFromDataRow(dr);
            string Xml = GetXml(oAssetHistory);
            return Xml;
        }

        public static string GetXml(DataTable dt)
        {
            if (dt == null) return null;
            AssetHistoryCls[] AssetHistoryCls = new AssetHistoryCls[dt.Rows.Count];
            for (int iIndex = 0; iIndex < dt.Rows.Count; iIndex++)
            {
                AssetHistoryCls[iIndex] = ParseFromDataRow(dt.Rows[iIndex]);
            }
            string Xml = GetXml(AssetHistoryCls);
            return Xml;
        }


        public static string GetXml(AssetHistoryCls[] AssetHistoryCls)
        {
            if (AssetHistoryCls == null)
            {
                //neu null thi khoi tao = mang rong de cho do loi...!
                AssetHistoryCls = new AssetHistoryCls[0];
            }
            DataSet ds = CreateDataset();
            for (int iIndex = 0; iIndex < AssetHistoryCls.Length; iIndex++)
            {
                AssetHistoryCls oAssetHistoryCls = AssetHistoryCls[iIndex];
                ds.Tables["Info"].Rows.Add(new object[]
                     {
                        oAssetHistoryCls.AssetListHistoryId,
                        oAssetHistoryCls.ActionDate,
                        oAssetHistoryCls.Subject,
                        oAssetHistoryCls.Owner,
                    });
            }
            string XmlData = ds.GetXml();
            return XmlData;
        }

        public static AssetHistoryCls[] ParseMultiRecordFromXml(string XmlData)
        {
            DataSet ds = XmlUtility.GetDataSetFromXml(XmlData);
            if (ds.Tables.Count == 0)
            {
                return new AssetHistoryCls[0];
            }
            AssetHistoryCls[] AssetHistoryCls = new AssetHistoryCls[ds.Tables[0].Rows.Count];
            for (int iIndex = 0; iIndex < ds.Tables[0].Rows.Count; iIndex++)
            {
                AssetHistoryCls[iIndex] = ParseFromDataRow(ds.Tables[0].Rows[iIndex]);
            }
            return AssetHistoryCls;
        }
    }
}
